package com.xiaotu.spider.tudou.pageprocessor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.HotTVModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.PropertiesUtil;
import com.xiaotu.spider.AUrlPageProcessor;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

/***
 * 
 * 项目名称：Sprider-WebMagic    
 * 类名称：TuDouHotPageProcessor    
 * 类描述：优酷网站热剧爬取    
 * 创建人：张晓   
 * 创建时间：2019年12月25日 下午12:12:05    
 * @version
 */
@Component("TuDouHotPageProcessor")
@Scope("prototype")
public class TuDouHotPageProcessor extends AUrlPageProcessor
{
	
    private static final String[] REQUEST_ARR = new String[]{
    		"http://category.tudou.com/category/c_97_s_4_p_%s.html",
    		"http://category.tudou.com/category/c_97_s_5_p_%s.html"
    		};// 排序方式：最受欢迎，最新上映

    @Override
    protected String[] getStartRequestUrls()
    {
    	int pageMaxSize = PropertiesUtil.getPropertyByInt(Constants.SpiderConfigKey.HOTTV_PAGE_SIZE);
    	List<String> startRequestList = new ArrayList<String>();
    	String [] startRequestArr = new String[pageMaxSize*2];
    	Arrays.asList(REQUEST_ARR).forEach(s -> {
    		for(int i=1;i<=pageMaxSize;i++){
    			startRequestList.add(String.format(s, i));
    		}
    	});
    	
        return startRequestList.toArray(startRequestArr);
    }

    
    @Override
    public void process(Page page)
    {
    	
    	List<HotTVModel> hotList = new ArrayList<HotTVModel>();
    	
    	List<String> tdList = page.getHtml().xpath("div[@class='td-row']//div[@class='td-col']").all();
    	if(null == tdList || tdList.isEmpty())
    		 throw new SpiderException(
                     SpiderExceptionCode.DataInvalideException);
    	
    	tdList.forEach(td -> {
    		
    		Html tdHtml = new Html(td);
    		
    		String tvName = tdHtml.xpath("div[@class='v-meta__title']/a/@title").get();
    		
    		String url = tdHtml.xpath("div[@class='v-meta__title']/a/@href").get();
    		
    		String actors = tdHtml.xpath("div[@class='v-meta__subtitle']/text()").get();
    		
    		List<String> actorList = this.getActors(actors);
    		
    		hotList.add(new HotTVModel(Constants.SiteName.TUDOU,tvName,actorList,url));
    		
    	});
    	
    	page.putField(HotTVModel.class.getName(), hotList);
    	
    }
    
    @Override
    public String getPageDataType(Page page)
    {
        return Constants.PageDataType.SEARCH_LIST;
    }

    
    private List<String> getActors(String actors)
    {
    	List<String> actorList = new ArrayList<String>();
    	if(null == actors || StringUtils.isBlank(actors)){
    		return actorList;
    	}
    	actors = actors.replace(" ", "").replace("主演：", "");
    	String [] actorArr = actors.split("/");
        for (int i = 0; i < actorArr.length; i++)
            actorList.add(actorArr[i]);
        return actorList;
    }

    @Override
    protected String getSpiderType()
    {
        return Constants.SpiderTaskType.YOUKU_HOT;
    }
    
}
